import React, {Component} from 'react';
import './App.css';
import Dashboard from "./components/Dashboard";
import Header from "./components/Layout/Header";
import "bootstrap/dist/css/bootstrap.min.css";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import AddProject from "./components/Project/AddProject";
import { Provider } from "react-redux";
import store from "./store";
import UpdateProject from "./components/Project/UpdateProject";
import ProjectBoard from "./components/ProjectBoard/ProjectBoard";
import AddProjectTask from "./components/ProjectBoard/ProjectTasks/AddProjectTask";
import UpdateProjectTask from "./components/ProjectBoard/ProjectTasks/UpdateProjectTask";
import Landing from "./components/Layout/Landing";
import Login from "./components/UserManagement/Login";
import Register from "./components/UserManagement/Register";
import jwt_decode from "jwt-decode";
import setJWTToken from "./securityUtils/setJWTToken";
import {SET_CURRENT_USER} from "./actions/types";
import { logout } from "./actions/securityActions";
import SecuredRoute from "./securityUtils/SecureRoute";

const jwtToken = localStorage.jwtToken;

if (jwtToken) {
    setJWTToken(jwtToken);
    const decodedToken = jwt_decode(jwtToken);
    store.dispatch({
        type: SET_CURRENT_USER,
        payload: decodedToken
    });

    const currentTime = Date.now() / 1000;
    if (decodedToken.exp < currentTime) {
        store.dispatch(logout());
        window.location.href = "/";
    }
}

class App extends Component {
  render() {
      return (
          <Provider store={store}>
            <Router>
                <div className="App">
                    <Header/>
                    {
                        // Public Routes
                    }
                    <Route exact
                           path="/"
                           component={Landing} />
                    <Route exact
                           path="/login"
                           component={Login} />
                    <Route exact
                           path="/register"
                           component={Register} />

                    {
                        // Private Routes
                    }
                    <Switch>
                    <SecuredRoute exact
                           path="/dashboard"
                           component={Dashboard}/>
                    <SecuredRoute exact
                           path="/addProject"
                           component={AddProject}/>
                    <SecuredRoute exact
                           path="/updateProject/:id"
                           component={UpdateProject}/>
                    <SecuredRoute exact
                           path="/projectBoard/:id"
                           component={ProjectBoard}/>
                    <SecuredRoute exact
                           path="/addProjectTask/:id"
                           component={AddProjectTask}/>
                    <SecuredRoute exact
                           path="/updateProjectTask/:backlog_id/:pt_id"
                           component={UpdateProjectTask}/>
                    </Switch>
                </div>
            </Router>
          </Provider>
      );
  }
}

export default App;
